package com.zzz.thrift.controller;

import com.zzz.thrift.service.api.TestService;
import com.zzz.thrift.service.pojo.Data;
import com.zzz.thrift.service.pojo.Detail;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.layered.TFastFramedTransport;
import org.apache.thrift.transport.layered.TFramedTransport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("test")
public class TestController {

    @Value("${thrift.service.secure.password:}")
    private String keyPass;

    @RequestMapping("test1")
    public void testUpload() {
        TTransport transport = null;
        TProtocol protocol = null;
        // 设置传输通道，对于非阻塞服务，需要使用TFramedTransport，它将数据分块发送
        try {
            transport = new TFastFramedTransport(new TSocket("127.0.0.1", 4567));
           // NACOS
            protocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "test-provider-server:TestService");
            if (!transport.isOpen()) {
                transport.open();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        TestService.Client client = new TestService.Client(protocol);
        Data data = genData(1, "张三", new String[]{"hello", "world"});
        try {
            client.upload(data);
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("test2")
    public void testUploadSecure() {
        String trustStore = this.getClass().getResource("/key/mytruststore.jks").getPath();
        if("\\".equals(File.separator)) {
            trustStore = trustStore.substring(1);
        }
        TSSLTransportFactory.TSSLTransportParameters parameters = new TSSLTransportFactory.TSSLTransportParameters();
        parameters.setTrustStore(trustStore, keyPass, "SunX509", "JKS");
        TTransport transport = null;
        TProtocol protocol = null;
        try {
            transport = new TFramedTransport(TSSLTransportFactory.getClientSocket("127.0.0.1", 5678, 30000, parameters));
            protocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "test-provider-server:TestService");
            if (!transport.isOpen()) {
                transport.open();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        TestService.Client client = new TestService.Client(protocol);
        Data data = genData(1, "张三", new String[]{"hello", "world"});
        try {
            client.upload(data);
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("test3")
    public void testBatchUpload() {
        TTransport transport = null;
        TProtocol protocol = null;
        // 设置传输通道，对于非阻塞服务，需要使用TFramedTransport，它将数据分块发送
        try {
//            transport = new TFramedTransport(TSSLTransportFactory.getClientSocket("127.0.0.1", 8090));
            transport = new TFastFramedTransport(new TSocket("127.0.0.1", 4567));
            // NACOS
            protocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "test-provider-server:TestService");
            if (!transport.isOpen()) {
                transport.open();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        TestService.Client client = new TestService.Client(protocol);
        List<Data> datas = new ArrayList<>();
        datas.add(genData(1, "zhangsan", new String[]{"hello", "world"}));
        datas.add(genData(2, "李四", new String[]{"你好吗？", "！@#￥%……&*（）——+"}));
        datas.add(genData(3, "王五", new String[]{"哈哈哈"}));
        try {
            client.batchUpload(datas);
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    private Data genData(int dataId, String name, String[] contents) {
        Data data = new Data();
        data.setId(dataId);
        data.setName(name);
        List<Detail> details = new ArrayList<>();
        for(String content : contents) {
            Detail detail = new Detail();
            detail.setDataId(dataId);
            detail.setContent(content);
            details.add(detail);
        }
        data.setDetailList(details);
        return data;
    }



}
